package com.central.kerberosManage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.central.common.annotation.LoginUser;
import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import com.central.common.model.DatasourceInfo;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import com.central.kerberosManage.model.HadoopClusterHost;
import com.central.kerberosManage.model.KeytabFile;
import com.central.kerberosManage.service.IKAMService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author Tindy
 * @date 2021/7/16
 * @describe
 */
@Slf4j
@RestController
@Api(tags = "kerberos用户管理模块api")
@RequestMapping(value = "/KAM")
public class KAMController {
    @Autowired
    private IKAMService kamService;

    @ApiOperation(value = "创建kerberos账号")
    @GetMapping("/createOrUpdateKA")
    public Result createOrUpdateKA(@ApiIgnore @LoginUser SysUser user) throws IOException, SQLException {
        kamService.createOrUpdateKA(user.getProName(),user.getUsername());
        return Result.succeed("创建成功");
    }

    @ApiOperation(value = "获取keytab文件url")
    @GetMapping("/getKeytabUrl")
    public KeytabFile getKeytabUrl(@ApiIgnore @LoginUser SysUser user) {
        return kamService.getOne(new QueryWrapper<KeytabFile>().eq("pro_name",user.getProName()).eq("username",user.getUsername()));
    }

    @ApiOperation(value = "调度中心master获取keytab文件url")
    @GetMapping("/pv/getKeytabUrl")
    public String getKeytabUrl(String proName,String username) {
        KeytabFile keytabFile = kamService.getOne(new QueryWrapper<KeytabFile>().eq("pro_name", proName).eq("username", username));
        if(keytabFile!=null){
            return keytabFile.getFileUrl();
        }else {
            return null;
        }
    }

    @ApiOperation(value = "批量添加大数据集群机器信息（用于创建用户系统账号）")
    @PostMapping("/addHadoopClusterHost")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result addHadoopClusterHosts(@RequestBody List<HadoopClusterHost> hostList){
        return kamService.addHadoopClusterHost(hostList);
    }

    @ApiOperation(value = "批量删除大数据集群机器信息")
    @DeleteMapping("/delHadoopClusterHost")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result delHadoopClusterHosts(@RequestBody List<Long> hostIds){
        return kamService.delHadoopClusterHost(hostIds);
    }

    @ApiOperation(value = "大数据集群机器信息列表,分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "host", value = "host", required = false, dataType = "String")
    })
    @GetMapping("/findHadoopClusterHosts")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public PageResult<HadoopClusterHost> findHadoopClusterHosts(@RequestParam Map<String, Object> params) {
        return kamService.findHadoopClusterHosts(params);
    }


    @ApiOperation(value = "刷新大数据集群机器系统用户")
    @GetMapping("/flushClusterSysAccount")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result flushClusterSysAccount() throws IOException {
        return kamService.flushClusterSysAccount();
    }

}
